<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>新建转诊门诊</title>

    <style>
        .margin-left-1 {
            margin-left: 10px !important;
        }

        .margin-left-2 {
            margin-left: 20px !important;
        }

        .margin-left-3 {
            margin-left: 30px !important;
        }

        .element-right {
            text-align: right;
        }

        .element-center {
            text-align: center;
        }

        .separate-line {
            width: 100%;
            height: 1px;
            background-color: black;
        }

        #lookup {
            text-decoration: underline;
            color: #6d9dcd;
        }


        #result-table th, #result-table td {
            border-collapse: collapse;
            text-align: center;
            border: 1px solid #ddd;
            min-height: 30px;
        }
        .btn-container {
            margin-top: 1em;
        }
        #add-medical span {
            color: red;
        }
    </style>

</head>

<body>

<div class="row from" id="">
    <div class="col-sm-12">
        <!--<h2 class="element-center">新建转诊门诊</h2>-->

        <div class="row margin-left-2" style="margin-top: 5px;">
            <div class="col-sm-10">
                <div class="">
                    <strong>
                        基本信息
                    </strong>
                </div>
            </div>
            <div class="col-sm-2 element-right">
                <!-- <a onclick="lookup()" id="lookup" style="margin-right: 20px;">查看病历</a> -->
                <a class="btn btn-primary btn-xs" onclick="referral_history()">查看转诊历史</a>
            </div>
        </div>

        <div class="row margin-left-2" style="margin-top: 10px;">
            <div class="col-sm-3">
                <span>患者编号：</span>
                <span id="EMR-num"></span>
            </div>
            <div class="col-sm-3">
                <span>姓名：</span>
                <span id="patient-name"></span>
            </div>
            <div class="col-sm-3">
                <span>性别：</span>
                <span id="patient-sex"></span>
            </div>
            <div class="col-sm-3">
                <span>年龄：</span>
                <span id="patient-age"></span>
            </div>
        </div>

        <div class="row margin-left-2" style="margin-top: 10px;">
            <div class="col-sm-3">
                <span>医保类型：</span>
                <span id="HIS-type"></span>
            </div>
            <div class="col-sm-3">
                <span>转诊医院：</span>
                <span id="out-hospital"></span>
            </div>
            <div class="col-sm-3">
                <span>转诊医生：</span>
                <span id="out-doctor"></span>
            </div>
            <div class="col-sm-3">
                <span>转诊时间：</span>
                <span id="out-time"></span>
            </div>
        </div>

        <div class="separate-line" style="margin-top: 10px;"></div>


        <div id="result-div" class="form-group hide">
           <h4>检索结果</h4>
           <table id="result-table" class="table table-bordered">
               <thead>
                   <tr class="active">
                        <th>病历号</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>年龄</th>
                        <th>医保类型</th>
                        <!-- <th>诊断</th> -->
                        <th>创建时间</th>
                   </tr>
               </thead>
               <tbody></tbody>
           </table>   
        </div>
        <div id="match-medical" class="hide text-right btn-container">
            <a class="btn btn-primary btn-sm" onclick="matchMedical()">匹配并新建病历</a>
        </div>
        <div id="add-medical" class="hide text-right btn-container">
			<span>没有和系统匹配的病历，请选择新建病历</span>
			<a class="btn btn-primary btn-sm" onclick="addMedical()">新建病历</a>
		</div>
    </div>

</div>


<script>

    var newEMR = {};
    var existEMR = {};
    var emrIdLookup = "";
    var in_emr_id = "";
    var transferId = $.getUrlParam("transfer_id");
    var tbody = $("#result-table tbody");
    var emrId = "";
    var type = 1;

    var add_params = {};
    add_params.referral_emr_id = transferId;
    add_params.in_emr = {};
    add_params.in_emr.patient = {};

    
    var id = "";

    $(document).ready(function () {
        if (transferId != "undefined" && transferId != null) {
            mandy_get_referral_emr(transferId, 1, function (data) {
                console.log("+++++++++++++++++++++++++++")
                console.log(data)
                console.log("---------------------------")
                id = data.out_emr.patient.identity_card;
                in_emr_id = data.out_emr.id;
                $("#EMR-num").text(data.out_emr.number);
                $("#patient-name").text(getPrivatePatientName(data.out_emr.patient.name));
                $("#patient-sex").text(sexArray[data.out_emr.patient.sex]);
                $("#patient-age").text(calculateAge(data.out_emr.patient.birthday));
                $("#HIS-type").text(HISTypeArray[data.out_emr.insurance_type]);
                $("#out-hospital").text(data.out_hospital.name);
                $("#out-doctor").text(data.send_medical.name);
                $("#out-time").text(new Date(data.out_time).format("YYYY-MM-DD HH:mm"));

                add_params.in_emr.number = data.out_emr.number;
                add_params.in_emr.insurance_type = data.out_emr.insurance_type;
                add_params.in_emr.patient.name = data.out_emr.patient.name;
                add_params.in_emr.patient.real_name = data.out_emr.patient.real_name;
                add_params.in_emr.patient.sex = data.out_emr.patient.sex;
                add_params.in_emr.patient.birthday = data.out_emr.patient.birthday;
                add_params.in_emr.patient.identity_card = id;
                filterEMR(id);
            }, function (err) {
                console.log(err)
            });
        }
    });
    
    
    function filterEMR(id) {
        if (id != null && id != "") {
            mandy_get_emr_by_identity_card(id, function (data) {
                if (data.id == null || data.id == "null"){
                    $("#result-div,#match-medical").addClass("hide");
                    $("#add-medical").removeClass("hide");
                    return;
                }else {
                    var td1 = $("<td>" + data.number + "</td>");
                    var td2 = $("<td>" + data.patient.name + "</td>");
                    var td3 = $("<td>" + getSex(data.patient.sex) + "</td>");
                    var td4 = $("<td>" + setAge(data.patient.birthday) + "</td>");
                    var td5 = $("<td>" + getInsuranceType(data.insurance_type) + "</td>");
                    var td6 = $("<td>" + moment(data.create_time).format("YYYY-MM-DD hh:mm") + "</td>");

                    var tr = $("<tr>").append(td1,td2,td3,td4,td5,td6);
                    tbody.append(tr);
                    emrId = data.id;
                    $("#result-div,#match-medical").removeClass("hide");
                    $("#add-medical").addClass("hide");
                }
            }, function (err) {
                $("#result-div,#match-medical").addClass("hide");
                $("#add-medical").removeClass("hide");
            });
        }else{
            $("#result-div,#match-medical").addClass("hide");
            $("#add-medical").removeClass("hide");
        }
    }

    function matchMedical(){
        mandy_post_match_emr(transferId,emrId,function(data){
            console.log("/////////////////////////////")
            console.log(data)
            console.log("/////////////////////////////")
            var params = {};
        	params.emr_id = emrId;
        	params.id_card = id; 
            params.transfer_id = transferId;
            params.type = type;
            setParamToUrl(params);
            loadTitle('/emr/info-title.html');
        },function(err){
            console.log(err)
        })
    }

    function addMedical(){
        mandy_post_add_emr(add_params,function(data){
            console.log("*********新建转诊病历*********")
            console.log(data)
            console.log("*********新建转折病历*********")
            var params = {};
            params.emr_id = data.id;
            params.transfer_id = transferId;
            params.type = type;
            setParamToUrl(params);
            loadTitle('/emr/info-title.html');
        },function(err){
            console.log(err)
        })

    }

    function lookup() {
        var param = {};
        param.emr_id = emrIdLookup;
        param.transfer_id = transferId;
        param.type = 1;
        setParamToUrl(param);
        loadTitle('/emr/info-title.html');
    }
    
    function referral_history(){
	 	var param = {};
	    param.emr_id = in_emr_id;
	    param.transfer_id = transferId;
		setParamToUrl(param);
		loadTitle('/referral/transfer_history_title.html');
    }
</script>


</body>

</html>
